<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div1{
            position: absolute;
            display: flex;
            flex-direction: column;
        }
        .div2{
            height: 600px;
            border-left: 2px solid red;
            position: absolute;
            left: 810px;
            /* background-color: antiquewhite; */
        }
        img{
            height: 150px;
            width: 150px;
            position: relative;
            /* left: 300px; */
            left: 0px;
            
        }
    </style>
</head>
<body>
    <div class="div1">
       <button id="btn1">开始！！</button>
       <button id="btn2">点击向左移动</button>
       <img src="../img/兔子2.gif" alt="" id="img"> 
       <img src="../img/乌龟.gif" alt="" id="img2">
    </div>
    <div class="div2">

    </div>
    
    <script>
        let btn = document.getElementById('btn1');
        let img = document.getElementById('img');
        let img2 = document.getElementById('img2');
        let btn2 = document.getElementById('btn2');
        let div2 = document.getElementsByClassName('div2')[0];
        // alert(div2.offsetLeft)
        // alert(img.offsetLeft)

        let timer1;
        let prm = new Promise((resolve,reject)=>{
            let click_btn = btn.addEventListener('click',()=>{
                move(img,5);
                move(img2,10);
            })
            resolve(click_btn);
        })
        prm.then((value)=>{
            value;
        },()=>{
            console.log('111');
        })

        btn2.onclick = function(){
            move(img,-10);
        }


        //定义一个人物移动的函数。
        //obj - 需要移动的对象
        //speed - 移动的速度
        function move(obj,speed){
            clearInterval(timer1);

            timer1 = setInterval(()=>{
                obj.style.left =obj.offsetLeft + speed +'px';
                if(obj.offsetLeft < 0){
                    obj.style.left = 0 +'px';
                    // alert(div2.offsetLeft- img.offsetLeft)
                }else if(obj.offsetLeft>=800){
                    obj.style.left = 800 +'px';
                }
            },30)
        }

    </script>
</body>
</html>